//2.用函数重载来实现第一题中的函数，重载三个分别找出  字符串数组  整型数组   double类型数组中的最大值or最小值。

#include <iostream>
#include <cstring>
using namespace std;

int findMaxOrMin(const int arr[], int size , bool isMax)
{
    if(size <= 0)
    {
        cout << "错误，数组要大于零" << endl;
        return 0;
    }

    int result = arr[0];
    for (int i = 1; i < size; i++)
    {
        if(isMax ? (arr[i]>result):(arr[i] < result))
        {
            result = arr[i];
        }
    }
    return result;
}

double findMaxOrMin(const double arr[], int size , bool isMax)
{
    if(size <= 0)
    {
        cout << "错误，数组要大于零" << endl;
        return 0;
    }

    double result = arr[0];
    for (int i = 1; i < size; i++)
    {
        if(isMax ? (arr[i]>result):(arr[i] < result))
        {
            result = arr[i];
        }
    }
    return result;
}

const char* findMaxOrMin(const char* arr[], int size, bool isMax) {
    if (size <= 0) {
        cerr << "错误：数组大小必须大于0" << endl;
        return "";
    }

    const char* result = arr[0];
    for (int i = 1; i < size; ++i) {
        if (isMax ? (strcmp(arr[i], result) > 0) : (strcmp(arr[i], result) < 0)) {
            result = arr[i];
        }
    }
    return result;
}

int main() {
    // 1. 测试整型数组
    int intArr[] = {3, 1, 4, 1, 5, 9, 2, 6};
    int intSize = sizeof(intArr) / sizeof(intArr[0]);
    cout << "整型数组最大值: " << findMaxOrMin(intArr, intSize, true) << endl;
    cout << "整型数组最小值: " << findMaxOrMin(intArr, intSize, false) << endl;

    // 2. 测试double数组
    double doubleArr[] = {3.5, 1.2, 4.8, 1.1, 5.9, 9.0, 2.3, 6.4};
    int doubleSize = sizeof(doubleArr) / sizeof(doubleArr[0]);
    cout << "double数组最大值: " << findMaxOrMin(doubleArr, doubleSize, true) << endl;
    cout << "double数组最小值: " << findMaxOrMin(doubleArr, doubleSize, false) << endl;

    // 3. 测试C风格字符串数组
    const char* strArr[] = {"apple", "banana", "orange", "grape", "pear"};
    int strSize = sizeof(strArr) / sizeof(strArr[0]);
    cout << "字符串数组最大值（字典序）: " << findMaxOrMin(strArr, strSize, true) << endl;
    cout << "字符串数组最小值（字典序）: " << findMaxOrMin(strArr, strSize, false) << endl;

    return 0;
}